<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
    <style>
        body {
            padding: 5px;
        }

        * {
            padding: 0;
            margin: 0;
        }

        img {
            vertical-align: top;
        }

        li {
            list-style: none;
        }

        a {
            text-decoration: none;
            color: inherit;
        }

        /* 计数框 */
        .countInput {
            height: 40px;
            line-height: 40px;
            text-align: center;
            display: inline-flex;
            border-radius: 2px;
            overflow: hidden;
        }

        .countInput .reduce,
        .countInput .add {
            width: 40px;
            background-color: #CCCCCC;
            color: #666666;
        }

        .countInput .input {
            width: 100px;
            outline: none;
            border: 1px solid #CCCCCC;
            text-align: center;
        }
    </style>
</head>

<body>
    <!-- vue的根实例(所有组件的父(代)组件) -->
    <div id="app">
        <h3>组件prop测试</h3>
        <div>
            <p>
                {{count}}
            </p>
            <button @click="count++">点击</button>
        </div>
        <br>
        <br>
        <br>
        <count-input :value="count"></count-input>

    </div>
</body>
<template id="countInput">
    <div class="countInput">
        <span class="reduce" @click="val--">-</span>
        <input class="input" :value="val" type="text">
        <span class="add" @click="val++">+</span>
    </div>
</template>
<script>

    // 此种写法:
    // 1. 父组件给子组件传递动态属性value => 子组件根据父组件传递的数据得到一个新值val,  点击加减修改的val  => 单纯的父->子通信  (子组件无法改变父组件的数据)
    // 缺点: 
    // 1. 子组件无法改变父组件的数据
    // 2. 父组件传入的value发生改变, 会覆盖val

    // 单向数据流  => 父组件可以修改子组件,子组件无法修改父组件上的数据

    Vue.component("count-input", {
        props: {
            value: {
                type: Number,
                default: 1,
            }
        },
        data() {
            return {
                val: this.value
            }
        },
        watch: {
            value(newValue) {
                this.val = newValue;
            }
        },
        template: "#countInput"
    })

    var vm = new Vue({
        el: "#app",
        data: {
            count: 1,

        },
        mounted() {

        },
    })





</script>

</html>